package com.guodi.map.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guodi.map.entity.MapMaterialFile;
import com.guodi.map.mapper.MapMaterialFileMapper;
import com.guodi.map.service.IMapMaterialFileService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @描述：
 * @作者: 戴伙进
 * @日期: 2020/6/3 9:29
 */
@Service
public class MapMaterialFileServiceImpl extends ServiceImpl<MapMaterialFileMapper, MapMaterialFile> implements IMapMaterialFileService {

    @Resource
    private MapMaterialFileMapper mapMaterialFileMapper;

    @Override
    public void deleteByParentId(String id) {
        mapMaterialFileMapper.deleteByParentId(id);
    }

    @Override
    public void deleteByIds(String id) {
        mapMaterialFileMapper.deleteByIds(id);
    }

    @Override
    public List<MapMaterialFile> listMaterialTreeByParentId(String parentId, String type) {
        QueryWrapper<MapMaterialFile> queryWrapper = new QueryWrapper();
        queryWrapper.select("*");
        queryWrapper.eq("parent_id",parentId);
        //进行排序
        queryWrapper.orderByAsc("type");
        queryWrapper.orderByAsc("sort");
        List<MapMaterialFile> allRootList = this.list(queryWrapper);
        QueryWrapper<MapMaterialFile> queryWrapper3 = new QueryWrapper();
        queryWrapper3.select("*");
        List<MapMaterialFile> allList = this.list(queryWrapper3);
        if(StringUtils.isNotBlank(type)){
            queryWrapper.eq("type",Integer.parseInt(type));
            allRootList = this.list(queryWrapper);
            QueryWrapper<MapMaterialFile> queryWrapper2 = new QueryWrapper();
            queryWrapper2.select("*");
            queryWrapper2.eq("type",Integer.parseInt(type));
            queryWrapper2.orderByAsc("type");
            queryWrapper2.orderByAsc("sort");
            allList = this.list(queryWrapper2);
        }
        return recursiveData(allRootList,allList);

    }


    @Override
    public List<MapMaterialFile> getAllByParentId(String parentId, String keyWord, Integer type) {
        return mapMaterialFileMapper.getAllByParentId(parentId,keyWord,type);
    }

    @Override
    public Integer selectMaxSort() {
        return mapMaterialFileMapper.selectMaxSort();
    }

    @Override
    public List<MapMaterialFile> getAllParentById(List<String> ids) {
        return mapMaterialFileMapper.getAllParentById(ids);
    }

    @Override
    public List<MapMaterialFile> getAllChildrenById(String id, Integer type) {
        return mapMaterialFileMapper.getAllChildrenById(id,type);
    }

    /**
     * @描述:递归生成目录树
     * @入参:
     * @出参:
     * @作者: 戴伙进
     * @日期: 2020/6/4 8:50
    **/
    public static List<MapMaterialFile> recursiveData(List<MapMaterialFile> rootList, List<MapMaterialFile> allList){
        if(rootList != null && rootList.size() > 0){
            for(MapMaterialFile mapMaterialFile : rootList){
                List<MapMaterialFile> childList = allList.stream().filter(p-> p.getParentId().equals(mapMaterialFile.getId())).collect(Collectors.toList());
                if (childList.size() > 0) {
                    mapMaterialFile.setChildren(childList);
                    recursiveData(childList, allList);
                }
            }
        }
        return rootList;
    }

}
